package ar.com.sne.model.composer.dom;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.zkoss.zk.ui.Component;

import foundation.common.util.BusinessException;
import foundation.zk.crud.composer.AbstractCRUDEditComposer;
import ar.com.sne.model.bo.dom.DomMercaderia;
import ar.com.sne.model.enumeration.dom.TipoMercaderia;
import ar.com.sne.model.service.dom.DomMercaderiaService;
import ar.com.sne.util.StringUtil;

@Service
@Scope("prototype")
public class EdicionMercaderiaComposer extends AbstractCRUDEditComposer {

	private final Log log = LogFactory.getLog(getClass());

	@Resource
	private DomMercaderiaService domMercaderiaService;
	
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}
	
	public Object[] getTipoMercaderiaList(){
		return TipoMercaderia.values();
	}
	
	public DomMercaderia getEditedObject() {
		return (DomMercaderia) super.getEditedObject();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * coop.foundation.zk.crud.composer.AbstractCRUDEditComposer#updateObject
	 * (java.lang.Object)
	 */
	
	@Override
	protected void updateObject(Object objectToUpdate) {
		DomMercaderia domMercaderia = (DomMercaderia)objectToUpdate;
		domMercaderia.setActivado("1");
		domMercaderiaService.save(domMercaderia);
	}

	
	@Override
	protected void verify(BusinessException be) {
		
		if(StringUtil.isNullOrEmpty(getEditedObject().getNombre())){
			be.addExceptionKey("EdicionMercaderiaComposer.validate.nombre.requerido");
		}
		if(this.getEditedObject().getTipo()==null){
			be.addExceptionKey("EdicionMercaderiaComposer.validate.tipo.requerido");
		}
	}
	
	
	@Override
	protected Object getNewObjectToAdd() {
		return new DomMercaderia();
	}


}